package com.example.demo.config;

import com.example.demo.commom.JWTUtils;
import com.example.demo.commom.TokenUtils;
import com.example.demo.entity.Userinfo;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-10-14
 * Time:22:34
 */
//登陆拦截器
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        if(JWTUtils.checkToken(token)) {
            return true;
        }
        //调整返回值，调到登录
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        response.sendRedirect("/login.html");
        return false;
    }
}
